package com.stud.postgres.util;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.stud.postgres.jdbc.GeometryJSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKBReader;
import org.postgis.PGgeometry;
import org.springframework.util.CollectionUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * @author Administrator
 * @date 2023-11-16 10:51:23
 * @description: TODO
 */
@Slf4j
public class Toolkit {

    /**
     * 此方法绝大多数都是插入时用的
     * 目前只能借助postgres数据库,java解析面时会报错:not_close;
     *
     * @param json {"type":"Point","coordinates":[4003637.462481571,38409112.070416726]}
     * @return 0101000020E61000009B9832BB9A8B4E41A53690C09C508241
     */
    public static String geometryJson2Binary(String json) {
//        MyJdbcTemplate jdbcTemplate = SpringBeanUtils.getBean("onemapJdbcTemplate");
//        return jdbcTemplate.findSingleColumn(SqlHelp.init("select st_geomfromgeojson(?)", json));
        return null;
    }

    /**
     * @param binary 0101000020E61000009B9832BB9A8B4E41A53690C09C508241
     * @return {"type":"Point","coordinates":[4003637.462481571,38409112.070416726]}
     */
    public static String geometryBinary2Json(String binary) {
        if (StringUtils.isBlank(binary)) {
            return null;
        }

        try {
            WKBReader reader = new WKBReader();
            Geometry geometry = reader.read(WKBReader.hexToBytes(binary));
            GeometryJSON geometryJson = new GeometryJSON(16);
            return geometryJson.toString(geometry);
        } catch (Exception e) {
            log.error("geometryBinary2Json解析失败:{}", binary);
            throw new RuntimeException("解析geometry失败");
        }
    }

    /**
     * @param binary 0101000020E61000009B9832BB9A8B4E41A53690C09C508241
     * @return POINT (4003637.462481571 38409112.070416726)
     */
    public static String geometryBinary2WBK(String binary) {
        if (StringUtils.isBlank(binary)) {
            return null;
        }
        try {
            PGgeometry pGgeometry = new PGgeometry(binary);
            return pGgeometry.getValue().replace("SRID=4326;", "");
        } catch (SQLException ignored) {
            throw new RuntimeException("解析geometry失败");
        }
    }

    /**
     * @param json {"type":"Point","coordinates":[4003637.462481571,38409112.070416726]}
     * @return POINT (4003637.462481571 38409112.070416726)
     */
    public static String geometryJson2WBK(String json) {
        return geometryJson2Binary(json);
//        try {
//            GeometryJSON geometryJSON = new GeometryJSON(16);
//            Geometry geometry = geometryJSON.read(new StringReader(json));
//            return geometry.toString();
//        } catch (IOException e) {
//            return null;
//        }
    }

    /**
     * {"type":"LineString","coordinates":[[4001484.209990108,38411831.78384369],[4001249.07085182,38412461.160424456]]}
     *
     * @param list
     * @return
     */
    public static String binary2Line(List<String> list) {
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }

        JSONObject line = new JSONObject();
        JSONArray coordinates = new JSONArray();
        line.put("type", "LineString");
        line.put("coordinates", coordinates);
        list.forEach(binary -> {
            JSONObject json = JSON.parseObject(geometryBinary2Json(binary));
            JSONArray array = json.getJSONArray("coordinates");
            coordinates.add(array);
        });
        return JSON.toJSONString(line);
    }

    public static void main(String[] args) throws ParseException, SQLException {
        String binary = "0103000020E61000000100000004000000F668DA3020894E413E8006D9E05082417DD3A2C44E884E41F390BAD6F1508241932793D5B18A4E4195F89AC30C5182413E5493CB838B4E4169E11501FD508241";
        String json = "{\"type\":\"Polygon\",\"coordinates\":[[[4002641.519150752,38411330.978303686],[4002285.4951166874,38411865.08934434],[4003681.1076355516,38412438.082174785],[4004276.1243515573,38411704.24735231]]]}";
        String wbk = "POINT (4003637.462481571 38409112.070416726)";

        System.out.println(geometryBinary2Json(binary));
//        System.out.println(geometryBinary2WBK(binary));
//        System.out.println(geometryJson2WBK(json));
//
//
//        WKBReader reader = new WKBReader();
//        Geometry geometry = reader.read(WKBReader.hexToBytes(binary));
//        GeometryJSON geometryJson = new GeometryJSON(16);
//        geometryJson.toString();


//        String point1 = "01010000008716D9B5F250824121B072D8E18C4E41";
//        String point2 = "0101000000D7A3705FF05082416DE7FB29E28C4E41";
//
//        System.out.println(binary2Line(Arrays.asList(point1, point2)));
    }


}
